package io.lkt.modules.sys.service;

import io.lkt.common.redis.RedisCacheUtil;
import io.lkt.modules.sys.dao.BArticleInfoDao;
import io.lkt.modules.sys.dao.BUserBrandInfoDao;
import io.lkt.modules.sys.dao.BUserFingerDao;
import io.lkt.modules.sys.dao.BUserInfoDao;
import io.lkt.modules.sys.entity.BArticleInfoEntity;
import io.lkt.modules.sys.entity.BUserBrandInfoEntity;
import io.lkt.modules.sys.entity.BUserFingerEntity;
import io.lkt.modules.sys.entity.BUserInfoEntity;
import io.lkt.modules.sys.wantedutils.ImageHelper;
import io.lkt.modules.sys.wantedutils.SimilarImageSearch;
import io.swagger.models.auth.In;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BUserInfoServiceTest2 {

    @Autowired
    BUserInfoDao bUserInfoDao;

    @Autowired
    BUserBrandInfoDao bUserBrandInfoDao;

    @Autowired
    BUserFingerDao bUserFingerDao;

    @Autowired
    RedisCacheUtil redisCacheUtil;

    @Test
    public void createUserFinger() throws Exception {

        //List<BUserInfoEntity> userList= bUserInfoDao.getNotFingerUser();
        List<BUserInfoEntity> userList= bUserInfoDao.getkdbjzzsq();
        for(BUserInfoEntity user:userList) {

            try {
                String path = "D:\\userImg20211215\\";
                String avatarName = UUID.randomUUID().toString() + ".jpg";
                ImageHelper.downloadAvatarPic(user.getAvatarUrl(), user.getUserId(), avatarName, path);
                String avatarPath = path + user.getUserId() + "\\" + avatarName;
                String hashCode = SimilarImageSearch.produceFingerPrint(avatarPath);

                BUserFingerEntity userFinger = new BUserFingerEntity();
                userFinger.setUserId(user.getUserId());
                userFinger.setGzhCode(user.getGzhCode());
                userFinger.setNickName(user.getNickName());
                userFinger.setOpenId(user.getOpenId());
                userFinger.setAvatarUrl(user.getAvatarUrl());
                userFinger.setAvaterFinger(hashCode);

                bUserFingerDao.insert(userFinger);

                FileUtils.forceDeleteOnExit(new File(avatarPath));
            } catch (Exception e) {
                System.out.println("UserId:" + user.getUserId() + "___AvatarUrl:" + user.getAvatarUrl() + "___e:" + e.getMessage());
            } finally {
                System.out.println("UserId:" + user.getUserId() + "___AvatarUrl:" + user.getAvatarUrl() + "___T:" + Thread.currentThread().getName());
            }
        }

//        ExecutorService pool = Executors.newFixedThreadPool(10);
//        // 始终只有10个线程来处理请求任务，来不及处理的任务会放置于阻塞队列中
//        List<BUserInfoEntity> userList = bUserInfoDao.getNotFingerUser();
//        for (BUserInfoEntity user : userList) {
//            pool.submit(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        String path = "D:\\userImg\\";
//                        String avatarName = UUID.randomUUID().toString() + ".jpg";
//                        ImageHelper.downloadAvatarPic(user.getAvatarUrl(), user.getUserId(), avatarName, path);
//                        String avatarPath = path + user.getUserId() + "\\" + avatarName;
//                        String hashCode = SimilarImageSearch.produceFingerPrint(avatarPath);
//
//                        BUserFingerEntity userFinger = new BUserFingerEntity();
//                        userFinger.setUserId(user.getUserId());
//                        userFinger.setGzhCode(user.getGzhCode());
//                        userFinger.setNickName(user.getNickName());
//                        userFinger.setOpenId(user.getOpenId());
//                        userFinger.setAvatarUrl(user.getAvatarUrl());
//                        userFinger.setAvaterFinger(hashCode);
//
//                        bUserFingerDao.insert(userFinger);
//
//                        FileUtils.forceDeleteOnExit(new File(avatarPath));
//                    } catch (Exception e) {
//                        System.out.println("UserId:"+user.getUserId()+"___AvatarUrl:"+user.getAvatarUrl()+"___e:"+e.getMessage());
//                    } finally {
//                        System.out.println("UserId:"+user.getUserId()+"___AvatarUrl:"+user.getAvatarUrl()+"___T:"+Thread.currentThread().getName());
//                    }
//                }
//            });
//        }
    }

    @Test
    public void updateUserBrandOrder() {
        String gzhCode = "prodbkwz";

        List<Integer> brandArr = new ArrayList<>();
        brandArr.add(17115);

        //把这个公众号所有人拿出来
        List<BUserInfoEntity> userList = bUserInfoDao.queryUserByGzhCode(gzhCode);
        //循环所有人
        for (BUserInfoEntity user : userList) {

            List<BUserBrandInfoEntity> userBrandList = bUserBrandInfoDao.queryUserBrandInfo(user.getUserId());

            if (userBrandList != null && userBrandList.size() > 0) {
                List<Integer> newUserBrand = new ArrayList<>();
                List<Integer> B_arr = new ArrayList<>();

                //把A、B品牌分开
                for (BUserBrandInfoEntity ub : userBrandList) {
                    if (!brandArr.contains(ub.getBrandId())) { //A品牌直接入列
                        newUserBrand.add(ub.getBrandId());
                    } else {//B品牌先放一个数组中
                        B_arr.add(ub.getBrandId());
                    }
                }

                for (Integer brandId : brandArr) {//按公众号品牌的顺序 把B品牌重新排列 并入列
                    if (B_arr.contains(brandId)) {
                        newUserBrand.add(brandId);
                    }
                }

                //先将用户当前所有品牌设置为无效
                bUserBrandInfoDao.updateUserBrandEffectiveStatus(user.getUserId());

                //把品牌的序号 重新排一遍
                for (int index = 0; index < newUserBrand.size(); index++) {
                    Integer bId = newUserBrand.get(index);

                    //查询用户对当前品牌的设置情况
                    BUserBrandInfoEntity queryInfo = new BUserBrandInfoEntity();
                    queryInfo.setUserId(user.getUserId());
                    queryInfo.setBrandId(bId);
                    BUserBrandInfoEntity brandStatusInfo = bUserBrandInfoDao.queryUserBrandStatusInfo(queryInfo);

                    //如果不存在，则直接添加
                    if (brandStatusInfo == null) {
                        BUserBrandInfoEntity addBrandInfo = new BUserBrandInfoEntity();
                        addBrandInfo.setBrandId(bId);
                        addBrandInfo.setUserId(user.getUserId());
                        addBrandInfo.setBrandStatus(0);
                        addBrandInfo.setEffectiveStatus(1);
                        addBrandInfo.setCreateTime(new Date());
                        addBrandInfo.setIndexNum(index);
                        bUserBrandInfoDao.addUserBrandInfo(addBrandInfo);
                    } else {
                        //如果品牌为无效状态，则进行修改
                        int effectiveStatus = Optional.ofNullable(brandStatusInfo.getEffectiveStatus()).orElse(0);
                        if (effectiveStatus == 0) {
                            BUserBrandInfoEntity updateBrandInfo = new BUserBrandInfoEntity();
                            updateBrandInfo.setId(brandStatusInfo.getId());
                            updateBrandInfo.setEffectiveStatus(1);
                            updateBrandInfo.setModifyTime(new Date());
                            updateBrandInfo.setIndexNum(index);
                            bUserBrandInfoDao.updateUserBrandInfo(updateBrandInfo);
                        }

                    }
                }

                System.out.println("用户：" + user.getUserId() + "已处理");
                if(newUserBrand.size()!=brandArr.size()){
                    System.out.println("用户：" + user.getUserId() + "已处理，关注品牌数："+newUserBrand.size());
                }
            }
        }
    }
}